﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Data;
using OgilvyOne.Model;
using OgilvyOne.Utility;
using OgilvyOne.DALFactory;

namespace OgilvyOne.BLL
{
    public class ArticleTemplateType : CommonModel
    {
        //protected IStaticGeneratableModel vo;
        //public IStaticGeneratableModel CmsVo
        //{
        //    get { return vo; }
        //}
        //内容
        protected CmsArticleModel article;
        //栏目相关
        //protected MenuModel menupage;
        //public MenuModel MenuModel
        //{
        //    get { return menupage; }
        //}
        ////公共方法相关
        //protected ContentHtmlPage htmlpage;
        //protected string html = ""; //模板HTML

        public ArticleTemplateType()
        {

        }
        public ArticleTemplateType(IStaticGeneratableModel vo) : base(vo)
        {
            //this.Model = vo;
            if (vo != null && vo.TemplateID.HasValue)
            {
                if (vo is CmsArticleModel)
                    this.article = (CmsArticleModel)vo;

                //this.html = new ModelManageBLL().getModel(vo.TemplateID.Value).TemCotent;
                //if (vo.ColumnID > 0)
                //{//存在栏目ID
                //    menupage = new MenuModel(vo.ColumnID.Value, vo.ID.Value);
                //}
                //else
                //{
                //    menupage = new MenuModel();
                //}
            }
            else
            {
                //menupage = new MenuModel();
            }
            //htmlpage = new ContentHtmlPage(menupage);
        }
        /// <summary>
        /// Cms 模板标签正则定义 形如: <cms:content value="GetTest"></cms:content>
        /// </summary>
        static Regex regex = new Regex("<cms:\\w+\\s+value=[\'|\"][\\w\\.\\s\\*]+[\'|\"][\\s\\w\'\"\\=]*>[\\[\\]\\&\\;\\!\'\"\\=<>/\\w\\s]*</cms:\\w+>");

        public override string GetHtml()
        {
            MatchCollection mc = regex.Matches(html);

            foreach (Match m in mc)
            {
                string mv = m.Value;
                html = StringUtil.ReplaceStr(html, mv, LabelProxy.GetCmsLabel(this, mv).GetLabelHtml());
            }

            return html;
        }

        /// <summary>
        /// 当前内容信息
        /// </summary>
        /// <returns></returns>
        public CmsArticleModel GetConInfo()
        {
            if (this.Model is CmsArticleModel)
                return (CmsArticleModel)this.Model;

            return null;
        }
        /// <summary>
        /// 当前栏目信息
        /// </summary>
        /// <returns></returns>
        public ColumnModel GetColumnInfo()
        {
            return menupage.GetColumn();
        }
     
        /// <summary>
        /// 父栏目信息
        /// </summary>
        /// <returns></returns>
        public ColumnModel GetParentColumnInfo()
        {
            return menupage.GetColumn(menupage.GetColumn().Parentid);
        }

        /// <summary>
        /// 当前内容标题
        /// </summary>
        /// <returns></returns>
        public string GetTitle()
        {
            if (GetConInfo() != null)
            {
                return GetConInfo().Title;
            }
            return "";
        }
        /// <summary>
        /// 副标题1
        /// </summary>
        /// <returns></returns>
        public string GetSubTitle()
        {
            if (this.article != null && this.article.Mores != null)
            {
                if (this.article.Mores.Subtitle != null && this.article.Mores.Subtitle != "")
                {
                    return "<h3>-" + this.article.Mores.Subtitle + "</h3>";
                }
            }
            return "";
        }
        /// <summary>
        /// 副标题2
        /// </summary>
        /// <returns></returns>
        public string GetSubTitle2()
        {
            if (this.article != null && this.article.Mores != null)
            {
                if (this.article.Mores.Subtitle2 != null && this.article.Mores.Subtitle2 != "")
                {
                    return "<h3>-" + this.article.Mores.Subtitle2 + "</h3>";
                }
            }
            return "";
        }

        /// <summary>
        /// 当前内容点击，收藏数
        /// </summary>
        /// <returns></returns>
        public string GetHitsColls(CmsArticleModel  cm)
        {

            return GetHitsCollsHtml(cm);
           
        }

        public static string GetHitsCollsHtml(CmsArticleModel cm)
        {
            string str = "点击<font class='hid' hid='" + cm.Articleid + "'>" + cm.Hits + "</font>次 收藏<font class='cid' cid='" + cm.Articleid + "'>" + cm.Colls + "</font>次";
            return str;
        }

        public string GetMenuListTitle()
        {
            return menupage.GetMenuListTitle();
          
        }
        /// <summary>
        /// 当前内容详细内容
        /// </summary>
        /// <returns></returns>
        public string GetContent()
        {
            if (this.article != null)
            {
                return this.article.Content;
            }
            return "";
        }
        /// <summary>
        /// 内容来源
        /// </summary>
        /// <returns></returns>
        public virtual string GetContentSource()
        {
            if (this.article != null)
            {
                if (this.article.Source != null && this.article.Source != "")
                {
                    string s =  this.article.Source ;
                    if (this.article.Sourceurl != null && this.article.Sourceurl != "")
                    {
                        s = "<a href=\"" + this.article.Sourceurl + "\" target=\"_blank\">" + s + "</a>";
                    }
                    return "<p>" + "（来源：" + s + "）</p>";
                }
            }
            return "";
        }
        public virtual string GetTitleImg()
        {
            if (this.Model.ID > 0)
            {
                IList imgs = CmsArticleFrontFactory.create().GetImgs(this.Model.ID.Value, CmsConstants.IMG_TYPE_TITLE);
                if (imgs.Count > 0)
                {
                    CmsArticleImgModel img = (CmsArticleImgModel)imgs[0];
                    return ConConstants.UPLOADFILES_IMAGE_PATH + img.ImgFileName;
                }
            }
            
            return "";
        }
        //普通图
        public string GetContentImg()
        {
            if (this.Model.ID > 0)
            {
                IList imgs = CmsArticleFrontFactory.create().GetImgs(this.Model.ID.Value, CmsConstants.IMG_TYPE_COMMON);
                if (imgs.Count > 0)
                {
                    CmsArticleImgModel img = (CmsArticleImgModel)imgs[0];
                    return ConConstants.UPLOADFILES_IMAGE_PATH + img.ImgFileName;
                }
            }
            return "";
        }

        public string GetTitleImgTag()
        {
            string img = GetTitleImg();
            if (img != "")
            {
                return "<img src=\"" + img + "\" />";
            }
            return "";
        }
        //title/meta/seo
        public virtual string GetMeta()
        {
            StringBuilder sb = new StringBuilder();
            ColumnModel cm = menupage.GetColumn(); //当前栏目
            
            if (this.article != null)
            {//内容SEO
                if (this.article.Articlekeyword != null && this.article.Articlekeyword.Trim() != "")
                {//有设定
                    sb.Append("<meta name=\"Keywords\" content=\"");
                    sb.Append(this.article.Articlekeyword);
                    sb.Append("\" />\n");

                    sb.Append("<meta name=\"Description\" content=\"");
                    sb.Append(this.article.Articledesc);
                    sb.Append("\" />\n");
                }
                else
                {//无设定
                    sb.Append("<meta name=\"Keywords\" content=\"");
                    sb.Append(this.article.Title);
                    sb.Append("\" />\n");

                    //sb.Append("<meta name=\"Description\" content=\"");
                    //sb.Append(ContentHtmlPage.GetSubString(this.article.Content, 100));
                    //sb.Append("\" />\n");
                    sb.Append(menupage.GetColumntSEO(false));
                }
                string code = GetContentMetaCode();
                if (code == null || code == "")
                {
                    code = menupage.CodeMeta();
                }
                sb.Append(code);
            }
            else
            {//栏目SEO
                sb.Append(menupage.GetColumntSEO(true));
            }
            return sb.ToString();
        }
        
        //栏目 Banner (kv)
        public string GetColumnBanner()
        {
            if (this.Model.ID > 0)
            {//文章banner
                IList imgs = CmsArticleFrontFactory.create().GetImgs(this.Model.ID.Value, CmsConstants.IMG_TYPE_KV);
                if (imgs.Count > 0)
                {
                    return ConConstants.UPLOADFILES_IMAGE_PATH + ((CmsArticleImgModel)imgs[0]).ImgFileName;
                }
            }
            return menupage.GetColumntTitleImg();
        }
        //public virtual string GetLeftNavi()
        //{
        //    return menupage.GetLeftNavi(); 
        //}

        //public virtual string GetEnLeftNavi()
        //{
        //    return menupage.GetEnLeftNavi();
        //}

        /// <summary>
        /// 发布日期
        /// </summary>
        /// <returns></returns>
        public string GetPubDate()
        {
            return ContentHtmlPage.GetArticlePubDate(this.article);
        }

        /// <summary>
        /// 内容页左边循环图片
        /// </summary>
        /// <returns></returns>
        public string GetArticleImgs()
        {
            return htmlpage.GetArticleImgs(this.Model.ID.Value);
        }
        /// <summary>
        /// 栏目下文章的Title图
        /// </summary>
        /// <returns></returns>
        public List<CmsArticleModel> GetArticleList()
        {
            List<CmsArticleModel> list = new List<CmsArticleModel>();
            IList articles = CmsArticleFrontFactory.create().GetArticleOrderList(this.Model.ColumnID.Value, 0);
            for (int i = 0; i < articles.Count; i++)
            {
                CmsArticleModel model = (CmsArticleModel)articles[i];
                list.Add(model);
            }
            return list;
        }

        public List<CmsArticleImgModel> GetArticleListImgs()
        {//?
           IList listimgs= CmsArticleFrontFactory.create().GetImgs(this.Model.ID.Value, CmsConstants.IMG_TYPE_COMMON);
           List<CmsArticleImgModel> list = new List<CmsArticleImgModel>();
           for (int i = 0; i < listimgs.Count; i++)
           {
               CmsArticleImgModel model = (CmsArticleImgModel)listimgs[i];
               list.Add(model);
           }
           return list;
        }
        
        public string GetArticleContentImg()
        {
            IList ilist = CmsArticleFrontFactory.create().GetImgs(this.Model.ID.Value, CmsConstants.IMG_TYPE_COMMON);
            if (ilist.Count > 0)
            {
                return "<img src=\"" + ConConstants.UPLOADFILES_IMAGE_PATH + ((CmsArticleImgModel)ilist[0]).ImgFileName + "\" />";
            }
            return "";
        }

        //////------- 取栏目第一篇文章 strat
        private CmsArticleModel firstArticle = null;

        public CmsArticleModel GetFirstArticle()
        {
            if (firstArticle == null)
            {
                int _cid = this.Model.ColumnID.Value;
                ColumnModel cm = menupage.GetColumn();
                if (cm.Templatedetail == 0)
                {//无内容
                    _cid = ((ColumnModel)menupage.GetSonColumn(_cid)[0]).Classid;
                }
                IList cs = CmsArticleFrontFactory.create().GetArticleOrderList(_cid, 1);
                if (cs.Count > 0)
                {
                    firstArticle = (CmsArticleModel)cs[0];
                    if (this.Model.Preview == 1)
                    {//预览
                        firstArticle = new CmsArticleBll().GetCmsArticleModel(firstArticle.Articleid);
                    }
                    else
                    {
                        firstArticle = CmsArticleFrontFactory.create().getCmsArticle(firstArticle.Articleid);
                    }
                }
            }
            return firstArticle;
        }

        public CmsArticleModel GetFirstNewsArticle()
        {
            if (firstArticle == null)
            {
                int _cid = this.Model.ColumnID.Value;
                ColumnModel cm = menupage.GetColumn();
                if (cm.Templatedetail == 0)
                {//无内容
                    _cid = ((ColumnModel)menupage.GetSonColumn(_cid)[0]).Classid;
                }
                IList cs = CmsArticleFrontFactory.create().GetArticleList(_cid, 1);
                if (cs.Count > 0)
                {
                    firstArticle = (CmsArticleModel)cs[0];
                    if (this.Model.Preview == 1)
                    {//预览
                        firstArticle = new CmsArticleBll().GetCmsArticleModel(firstArticle.Articleid);
                    }
                    else
                    {
                        firstArticle = CmsArticleFrontFactory.create().getCmsArticle(firstArticle.Articleid);
                    }
                }
            }
            return firstArticle;
        }

        public string GetFirstNewsArticleUrl()
        {
            if (GetFirstNewsArticle() != null)
            {
                return menupage.GetArticleUrl(GetFirstNewsArticle());
            }
            return "";
        }

        /// <summary>
        /// 栏目第一篇文章URL
        /// </summary>
        /// <returns></returns>
        public string GetFirstArticleUrl()
        {
            if (GetFirstArticle() != null)
            {
                return menupage.GetArticleUrl(GetFirstArticle());
            }
            return "";
        }

        public string GetFirstTitle()
        {
            if (GetFirstArticle() != null)
            {
                return GetFirstArticle().Title;
            }
            return "";
        }
        public string GetFirstContent()
        {
            if (GetFirstArticle() != null)
            {
                return GetFirstArticle().Content;
            }
            return "";
        }
        public string GetFirstTitleImg()
        {
            if (GetFirstArticle() != null)
            {
                string img = GetFirstArticle().Titleimg;
                if (img != null && img != "")
                {
                    return "<img src=\"" + ConConstants.UPLOADFILES_IMAGE_PATH + img + "\" />";
                }
            }
            return "";
        }
        public string GetFirstArticleImgs()
        {
            return htmlpage.GetArticleImgs(GetFirstArticle().Articleid);
        }
        public string GetFirstPubDate()
        {
            return ContentHtmlPage.GetArticlePubDate(GetFirstArticle());
        }

        public string GetFirstArticleContentImg()
        {
            IList ilist = CmsArticleFrontFactory.create().GetImgs(GetFirstArticle().Articleid, CmsConstants.IMG_TYPE_COMMON);
            if (ilist.Count > 0)
            {
                return "<img src=\"" + ConConstants.UPLOADFILES_IMAGE_PATH + ((CmsArticleImgModel)ilist[0]).ImgFileName + "\" />";
            }
            return "";
        }
        //////-----------------------end

        public string GetColumnTitleImg()
        {
            ColumnModel cm = menupage.GetColumn();
            if (cm.Ttile_img != null && cm.Ttile_img != "")
            {
                return "<img src=\""+ConConstants.UPLOADFILES_IMAGE_PATH+ cm.Ttile_img + "\" />";
            }
            return "";
        }

        public string GetContentMetaCode()
        {
            if (this.article.Mores != null && this.article.Mores.Mores != null)
            {
                string s = this.article.Mores.GetMoresInfo("metacode");
                return s != null ? s : "";
            }
            return "";
        }

        public string GetVideoFilePath()
        {
            return ConConstants.VIDEO_FILE_PATH;
        }
    }
}
